import time

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://baidu.com/")
# 1、根据id定位 value值一定要加#
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("selenium")
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# 2、根据classname定位，value值一定要加.
# driver.find_element(By.CSS_SELECTOR, ".nav-search-input").send_keys("2024新年快乐")
# 3、根据name属性值定位,要加[name=""]，注意单双引号使用
# driver.find_element(By.CSS_SELECTOR, '[name="wd"]').send_keys("selenium")
# 4、根据标签属性定位
# driver.find_element(By.CSS_SELECTOR, "a[href='http://image.baidu.com/']").click()
# 5、模糊匹配-包含
# driver.find_element(By.CSS_SELECTOR, "a[href*='image.baidu.com']").click()  # 代表包含这一部分
# 6、模糊匹配-匹配开头
# driver.find_element(By.CSS_SELECTOR, "a[href^='http://image.baidu']").click()
# 7、模糊匹配-匹配结尾
# driver.find_element(By.CSS_SELECTOR, "a[href$='image.baidu.com/']").click()
# 8、组合定位 如果id用# classname用. name用[name=""]
# driver.find_element(By.CSS_SELECTOR, "input#kw").send_keys("2024新年快乐")
# 定位子元素 first-child last-child nth-child(序号) 序号从1开始 “>”代表按照顺序定位
# driver.find_element(By.CSS_SELECTOR, "#s-top-left>a").click()
# driver.find_element(By.CSS_SELECTOR, "div.s-top-left-new.s-isindex-wrap>a")  # class属性中间有空格记得用.
# driver.find_element(By.CSS_SELECTOR, "div#s-top-left>a:nth-child(3)").click()
# driver.find_elements(By.CSS_SELECTOR, "#s-top-left>a")[3].click()
# driver.find_element(By.CSS_SELECTOR, "#s-top-left>a:first-child").click()
driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(4)").click()

time.sleep(3)
driver.close()
